package com.learn.algorithm;

import java.util.Stack;

/**
 * 使用栈实现队列
 * 原则
 * 1. out空了才能从in取数据
 * 2. 如果取数据 一次性要取到in为空
 */
public class StackToQueue {
    public Stack<Integer> in;
    public Stack<Integer> out;

    public StackToQueue() {
        in = new Stack<>();
        out = new Stack<>();
    }

    public void inToOut() {
        if (out.isEmpty()) {
            while (!in.isEmpty()) {
                out.push(in.pop());
            }
        }
    }

    public void push(int x) {
        in.push(x);
        inToOut();
    }

    public int pop() {
        inToOut();
        return out.pop();
    }

    public int peek() {
        inToOut();
        return out.peek();
    }

    public boolean empty() {
        return in.isEmpty() && out.isEmpty();
    }
}
